package com.ruoyi.web.controller.equ;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.equ.domain.EquScrap;
import com.ruoyi.equ.domain.vo.EquScrapVos;
import com.ruoyi.equ.service.IEquScrapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 报废记录Controller
 *
 * @author ruoyi
 * @date 2025-11-05
 */
@RestController
@RequestMapping("/equ/scrap")
public class EquScrapController extends BaseController
{
    @Autowired
    private IEquScrapService equScrapService;

    /**
     * 查询报废记录列表
     */
    @PreAuthorize("@ss.hasPermi('equ:scrap:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ModelAttribute EquScrap equScrap)
    {
        startPage();
        List<EquScrap> list = equScrapService.selectEquScrapList(equScrap);
        return getDataTable(list);
    }
    /**
     * 查询报废记录列表
     */
    @PreAuthorize("@ss.hasPermi('equ:scrap:list')")
    @GetMapping("/listVo")
    public TableDataInfo listVo(EquScrapVos equScrap)
    {
        startPage();
        List<EquScrapVos> list = equScrapService.selectEquScrapVoList(equScrap);
        return getDataTable(list);
    }
    /**
     * 导出报废记录列表
     */
    @PreAuthorize("@ss.hasPermi('equ:scrap:export')")
    @Log(title = "报废记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ModelAttribute EquScrap equScrap)
    {
        List<EquScrap> list = equScrapService.selectEquScrapList(equScrap);
        ExcelUtil<EquScrap> util = new ExcelUtil<EquScrap>(EquScrap.class);
        util.exportExcel(response, list, "报废记录数据");
    }

    /**
     * 获取报废记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('equ:scrap:query')")
    @GetMapping(value = "/{scrapId}")
    public AjaxResult getInfo(@PathVariable("scrapId") Long scrapId)
    {
        return success(equScrapService.selectEquScrapByScrapId(scrapId));
    }

    /**
     * 新增报废记录
     */
    @PreAuthorize("@ss.hasPermi('equ:scrap:add')")
    @Log(title = "报废记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquScrap equScrap)
    {
        return toAjax(equScrapService.insertEquScrap(equScrap));
    }

    /**
     * 修改报废记录
     */
    @PreAuthorize("@ss.hasPermi('equ:scrap:edit')")
    @Log(title = "报废记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquScrap equScrap)
    {
        return toAjax(equScrapService.updateEquScrap(equScrap));
    }

    /**
     * 删除报废记录
     */
    @PreAuthorize("@ss.hasPermi('equ:scrap:remove')")
    @Log(title = "报废记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{scrapIds}")
    public AjaxResult remove(@PathVariable("scrapIds") Long[] scrapIds)
    {
        return toAjax(equScrapService.deleteEquScrapByScrapIds(scrapIds));
    }

    /**
     * 2.7新增报废记录并修改设备状态
     */
    @Log(title = "报废记录", businessType = BusinessType.INSERT)
    @PostMapping("/finishScrapAndAsset")
    public AjaxResult finishScrapAndAsset(@RequestBody EquScrap equScrap)
    {
        try {
            Boolean result =equScrapService.finishScrapAndAsset(equScrap);
            return AjaxResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }
    }
    /**
     * 查询报废记录列表
     */
    @PreAuthorize("@ss.hasPermi('equ:scrap:list')")
    @GetMapping("/getScrapByAssetId/{assetId}")
    public TableDataInfo getScrapByAssetId(@PathVariable("assetId") Long assetId)
    {
        startPage();
        List<EquScrap> list = equScrapService.getScrapsByAssetId(assetId);
        return getDataTable(list);
    }
}
